

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.Vector;

/**
 * A Wire object, composed of multiple wire segments which will
 * be drawn to the workspace. 
 * @author holde1ja
 */
public class Wire implements WorkspaceObj {
	private WirePin input;
	private Vector<WirePin> outputs;
	private Vector<Line2D> parts;
	private int bitWidth;
	private int sizeOfWire;
	
	/**
	 * Initializes the wire with empty input/output pins.  Also
	 * sets up the individual wire 'parts' which are used to draw
	 * the wire out on the GUI.
	 */
	public Wire()
	{
		input = null;
		outputs = new Vector<WirePin>();
		parts = new Vector<Line2D>();
		bitWidth = 1;
		sizeOfWire = 0;
	}
	
	/**
	 * Draws each of the wire's lines to the given graphics context
	 * @param g the graphics context to draw the wire to.
	 */
	public void draw(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		for (int i = 0; i < parts.size(); i++)
		{
			g2.draw(parts.get(i));
		}
	}

	/**
	 * 
	 * @return true if the wire is connected to the given attachable object
	 */
	@Override
	public boolean isConnected(Pin other) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	/**
	 * Attaches the wire to the given pin.  A wire can only attach
	 * to another chip, not to another wire.
	 * TODO: What do we do if the given chip
	 * @return true if we successfully attached, else false
	 */
	public boolean attachTo(Pin other) {
		if (!(other instanceof ChipPin))
			return false;
		
		ChipPin chipPin = (ChipPin)other;
		WirePin ourPin = new WirePin("", this, chipPin);
		
		//If the chipPin is an input pin, that means ourPin must be
		//our outputs
		if (chipPin.isInput())
		{
			outputs.add(ourPin);
		}
		else if (chipPin.isOutput())
		{
			//TODO: What if input was already pointing to something?
			//Spencer: if(this.input != null) return false;
			input = ourPin;
		}
		return true;
	}

	@Override
	public Boolean attachTo(Pin ourPin, Pin theirPin) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean detachFrom(Pin ourPin, Pin theirPin) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void select() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deselect() {
		// TODO Auto-generated method stub
		
	}
	
}
